﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace FasterFood
{
    public partial class SidesPage : UserControl
    {
        List<mealItem> mealsMenu = new List<mealItem>();
        List<Button> mealButtons = new List<Button>();
        //List
        bool bacon = true;
        bool cheese = true;
        bool mayo = true;
        bool onion = true;
        bool tomato = true;
        bool lettuce = true;
        public string currentItem = "";


        public SidesPage()
        {
            InitializeComponent();
            getMeals();//Read in meal data
            setButtons();//Display buttons
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1.foodPages[5].Visible = false;
            Form1.foodPages[1].Visible = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {

        }
        private void mealSelectedEvent(object sender, EventArgs e)
        {
            for (int i = 0; i < mealsMenu.Count(); i++)
            {
                if (sender.ToString().Contains(mealsMenu.ElementAt(i).getTitle()))
                {
                    //Set display information for item here
                    groupBox.Text = mealsMenu.ElementAt(i).getTitle();
                    descLbl.Text = mealsMenu.ElementAt(i).getDesc();
                    priceLbl.Text = "Price: " + mealsMenu.ElementAt(i).getPrice();
                    calsLbl.Text = "Calories: " + mealsMenu.ElementAt(i).getCalories();
                    string image = "";
                    if (mealsMenu.ElementAt(i).getImage() == "")
                        image = " ../../images/noPhoto.jpg";
                    else
                    {
                        try
                        {
                            image = "../../images/";
                            image = image + mealsMenu.ElementAt(i).getImage();
                        }
                        catch (Exception)
                        {
                            image = "../../images/noPhoto.jpg";
                        }
                    }
                    try
                    {
                        pictureBox1.Image = System.Drawing.Image.FromFile(image);
                    }
                    catch (Exception)
                    {
                        pictureBox1.Image = System.Drawing.Image.FromFile("../../images/noPhoto.jpg");
                    }
                    currentItem = "../../images/buttons/comboMenu/" + mealsMenu.ElementAt(i).getImage();
                    resetOptions();
                    infoPanel.Visible = true;
                }
            }
        }

        private void resetOptions()
        {
            this.bacon = true;
            this.cheese = true;
            this.mayo = true;
            this.onion = true;
            this.tomato = true;
            this.lettuce = true;

        //    button2.Image = System.Drawing.Image.FromFile("../../images/options/tomato.png");
        //    button1.Image = System.Drawing.Image.FromFile("../../images/options/bacon.png");
        //    button3.Image = System.Drawing.Image.FromFile("../../images/options/mayo.png");
        //    button6.Image = System.Drawing.Image.FromFile("../../images/options/cheese.png");
        //    button7.Image = System.Drawing.Image.FromFile("../../images/options/lettuce.png");
        //    button8.Image = System.Drawing.Image.FromFile("../../images/options/onion.png");
        }

        private void setButtons()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MealsMenu));
            int y = 3;
            for (int i = 0; i < mealsMenu.Count(); i++)
            {
                mealItem theMeal = mealsMenu.ElementAt(i);
                mealButtons.Add(new System.Windows.Forms.Button());
                mealButtons.ElementAt(i).Click += new System.EventHandler(this.mealSelectedEvent);
                mealButtons.ElementAt(i).Location = new System.Drawing.Point(3, y);
                mealButtons.ElementAt(i).Size = new System.Drawing.Size(344, 103);
                mealButtons.ElementAt(i).TabIndex = 0;
                mealButtons.ElementAt(i).Text = theMeal.getTitle();
                mealButtons.ElementAt(i).UseVisualStyleBackColor = true;
                try
                {
                    string buttonPath = "../../images/buttons/comboMenu/";
                    buttonPath += mealsMenu.ElementAt(i).getImage();
                    mealButtons.ElementAt(i).Image = System.Drawing.Image.FromFile(buttonPath);
                    string text = mealButtons.ElementAt(i).Text;
                    mealButtons.ElementAt(i).Text = "";
                    //YOU SAW NOTHING                    
                    for (int j = 0; j < 50; j++)
                        mealButtons.ElementAt(i).Text += "                                                                                                                                     ";
                    mealButtons.ElementAt(i).Text = text;
                    mealButtons.ElementAt(i).Font = new System.Drawing.Font("Microsoft Sans Serif", 0.1F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                }
                catch (Exception) { }
                this.flowLayoutPanel1.Controls.Add(mealButtons.ElementAt(i));
                y += 109;
            }
        }

        public void getMeals()
        {
            XDocument xdoc = XDocument.Load("menu.xml");
            var items = from item in xdoc.Descendants("SidesDrinks")
                        select new
                        {
                            Header = item.Attribute("name").ToString(),
                            Children = item.Descendants("side"),
                        };
            foreach (var item in items)
                foreach (var entry in item.Children)
                {
                    string title = "";
                    string desc = "";
                    string price = "";
                    string image = "";
                    string calories = "";

                    try { title = entry.Attribute("name").ToString(); }
                    catch (NullReferenceException) { title = "(Not Available)"; }

                    try { desc = entry.Attribute("desc").ToString(); }
                    catch (NullReferenceException) { desc = "(Not Available)"; }

                    try { price = entry.Attribute("price").ToString(); }
                    catch (NullReferenceException) { price = "(Not Available)"; }

                    try { image = entry.Attribute("image").ToString(); }
                    catch (NullReferenceException) {/* image = "(Not Available)"; */}

                    try { calories = entry.Attribute("calories").ToString(); }
                    catch (NullReferenceException) { calories = "(Not Available)"; }
                    mealsMenu.Add(new mealItem(title, desc, price, image, calories));
                }


            items = from item in xdoc.Descendants("SidesDrinks")
                    select new
                    {
                        Header = item.Attribute("name").ToString(),
                        Children = item.Descendants("drink"),
                    };
            foreach (var item in items)
                foreach (var entry in item.Children)
                {
                    string title = "";
                    string desc = "";
                    string price = "";
                    string image = "";
                    string calories = "";

                    try { title = entry.Attribute("name").ToString(); }
                    catch (NullReferenceException) { title = "(Not Available)"; }

                    try { desc = entry.Attribute("desc").ToString(); }
                    catch (NullReferenceException) { desc = "(Not Available)"; }

                    try { price = entry.Attribute("price").ToString(); }
                    catch (NullReferenceException) { price = "(Not Available)"; }

                    try { image = entry.Attribute("image").ToString(); }
                    catch (NullReferenceException) {/* image = "(Not Available)"; */}

                    try { calories = entry.Attribute("calories").ToString(); }
                    catch (NullReferenceException) { calories = "(Not Available)"; }
                    mealsMenu.Add(new mealItem(title, desc, price, image, calories));
                }
        }

        public class mealItem
        {
            public string title { get; set; }
            public string desc { get; set; }
            public string price { get; set; }
            public string image { get; set; }
            public string calories { get; set; }

            public string getTitle()
            {
                return title;
            }

            public string getDesc()
            {
                return desc;
            }

            public string getPrice()
            {
                return price;
            }

            public string getImage()
            {
                return image;
            }

            public string getCalories()
            {
                return calories;
            }
            public mealItem(string title, string desc, string price, string image, string calories)
            {
                try { this.title = title.Split('"')[1]; }
                catch (IndexOutOfRangeException) { this.title = "(Not Available)"; }

                try { this.desc = desc.Split('"')[1]; }
                catch (IndexOutOfRangeException) { this.desc = "(Not Available)"; }

                try { this.price = price.Split('"')[1]; }
                catch (IndexOutOfRangeException) { this.price = "(Not Available)"; }

                try { this.calories = calories.Split('"')[1]; }
                catch (IndexOutOfRangeException) { this.calories = "(Not Available)"; }

                try
                {
                    if (image.Split('"')[1] == "")
                        this.image = "";
                    else
                        this.image = image.Split('"')[1];
                }
                catch (IndexOutOfRangeException) { this.calories = "(Not Available)"; }
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            Form1.foodPages[5].Visible = false;
            Form1.foodPages[1].Visible = true;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            Form1.foodPages[5].Visible = false;
            Form1.foodPages[6].Visible = true;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (groupBox.Text.Length < 3)
            {
                return;
            }

            double price = double.Parse(priceLbl.Text.Substring(8));
            Form1.theCart.Add(new cartItem(groupBox.Text, price, currentItem));

            Form1.foodPages[9].Visible = true;
            Form1.foodPages[9].BringToFront();
        }

        private void startOverBtn_Click(object sender, EventArgs e)
        {
            // hide current page and start over
            Form1.foodPages[5].Visible = false;
            Form1.foodPages[0].Visible = true;
        }



    }
}
